﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreFrame.Business;
using MarchEstate.Manager.Dao.Cash;
using System.Data;
using System.Collections;
using MarchEstate.Manager.BusinesEntity;
using MarchEstate.Manager.BusinesEntity.Costcalculate;
using CoreFrame.Core.Aop;
using CoreFrame.Core.Transaction;

namespace MarchEstate.Manager.ObjectModel.Cash
{
    public class CashManager : AbstractGenericModel
    {
        private CashManagerDao cashDao = null;

        public CashManager()
        {
        }

        public override void InitDao()
        {
            base.InitDao();
            cashDao = NewDao<CashManagerDao>();
        }

        public override object OperationDataModel(string method, object value)
        {
            switch (method)
            {
                case "GetFloorData":
                    return GetRoomData(value);
                case "GetSrccitemData":
                    return cashDao.GetSrccitemData(value.ToString());
                case "GetitemHeadData":
                    return GetitemHeadData(value);
                case "DeleteItemFeeHead":
                    return cashDao.DeleteItemFeeHead(value.ToString());
                case "SaveItemFeeHead0":
                    return SaveItemFeeHead0(value);
                case "SaveItemFeeHead2":
                    return SaveItemFeeHead2(value);
                case "GetSrccInfo":
                    return cashDao.GetSrccInfo(value.ToString());
            }

            return null;
        }

        private DataTable GetRoomData(object value)
        {
            Hashtable hash = (Hashtable)value;
            if (hash["type"].ToString() == "1")//住宅
            {
                return cashDao.GetRoomData(hash["areacode"].ToString(), hash["buildcode"].ToString(), Convert.ToInt32(hash["usestatus"]));
            }
            else if (hash["type"].ToString() == "2")//门面
            {
                return cashDao.GetShopData();
            }
            else if (hash["type"].ToString() == "3")//停车位
            {
                return cashDao.GetCarportData();
            }
            else if (hash["type"].ToString() == "4")//客户
            {
                return cashDao.GetClienteleData();
            }
            return null;
        }

        private DataTable GetitemHeadData(object value)
        {
            string headId = ((Hashtable)value)["HeadId"].ToString();
            string srccId=((Hashtable)value)["srccId"].ToString();
            string itemcode = ((Hashtable)value)["itemcode"].ToString();
            return cashDao.GetitemHeadData(headId,srccId, itemcode);
        }
        [AOP(typeof(AopTransaction))]
        private object SaveItemFeeHead0(object value)
        {
            string SrccId = ((Hashtable)value)["SrccId"].ToString();
            string ItemCode = ((Hashtable)value)["ItemCode"].ToString();
            DateTime Thismaturity = Convert.ToDateTime(((Hashtable)value)["Thismaturity"]);

            AbstractOSRCCItemRelation osrcc = NewObject<AbstractOSRCCItemRelation>();
            AbstractOSRCCItemRelation _srcc = osrcc.GetSrccItem(SrccId, ItemCode);
            
            DataRow dr = cashDao.GetSrccInfo(SrccId);
            AbstractServiceItem serviceitem = NewObject<AbstractServiceItem>();
            AbstractServiceItem _serviceitem = serviceitem.GetServiceItem(ItemCode);
            AbstractResRateTime res = NewObject<AbstractResRateTime>("restime");
            res.LastDate = _srcc.Lastmaturity.Date;
            res.CurrentDate = Thismaturity.Date;
            res.Unitprice = _serviceitem.Price;
            res.Usearea = Convert.ToDecimal(dr["UseArea"]);

            List<object[]> segvalue;
            decimal ThisCostFee = res.GetItemFee(out segvalue);
            decimal ThisLateFee = res.GetLateFee();
            if (ThisCostFee != Convert.ToDecimal(((Hashtable)value)["ThisCostFee"]) || ThisLateFee != Convert.ToDecimal(((Hashtable)value)["ThisLateFee"]))
                throw new Exception("预算的费用有误，请调整本次到期日重新预算！");
            decimal FavourFee = Convert.ToDecimal(((Hashtable)value)["FavourFee"]);
            decimal RealityFee = Convert.ToDecimal(((Hashtable)value)["RealityFee"]);
            //更新OSRCCItem显示数据
            osrcc.UpdateThisFee(SrccId, ItemCode,Thismaturity, ThisCostFee, ThisLateFee, FavourFee, RealityFee, 0);

            //更新ItemFeeHead,删除原来所有的费用记录，再重新插入

            cashDao.DeleteItemFeeHeadAndOrder(SrccId, ItemCode);

            AbstractItemFeeHead head = NewObject<AbstractItemFeeHead>();
            AbstractItemFeeOrder feeorder = NewObject<AbstractItemFeeOrder>();

            for (int i = 0; i < segvalue.Count; i++)
            {
                AbstractItemFeeHead _head = (AbstractItemFeeHead)head.Clone();
                _head.OwnerId = _srcc.OwnerId;
                _head.OwnerName = dr["OwnerName"].ToString();
                _head.SrccId = SrccId;
                _head.ItemCode = ItemCode;
                _head.ItemName = _srcc.ItemName;
                _head.Lastmaturity = Convert.ToDateTime(segvalue[i][0]);
                _head.Thismaturity = Convert.ToDateTime(segvalue[i][1]);
                _head.Price = _serviceitem.Price;
                _head.DosageNum = 1;
                _head.CostFee = Convert.ToDecimal(segvalue[i][2]);
                _head.LateFee = 0;//?
                _head.save();
                List<object[]> dayvalue = (List<object[]>)segvalue[i][3];
                for (int j = 0; j < dayvalue.Count; j++)
                {
                    AbstractItemFeeOrder _feeorder = (AbstractItemFeeOrder)feeorder.Clone();
                    _feeorder.OrderID = null;
                    _feeorder.HeadId = _head.HeadId;
                    _feeorder.OwnerId = _srcc.OwnerId;
                    _feeorder.OwnerName = dr["OwnerName"].ToString();
                    _feeorder.SrccId = SrccId;
                    _feeorder.ItemCode = ItemCode;
                    _feeorder.ItemName = _srcc.ItemName;
                    _feeorder.StatCode = _serviceitem.StatCode;
                    _feeorder.OrderDate = Convert.ToDateTime(dayvalue[j][0]);
                    _feeorder.TotalFee = Convert.ToDecimal(dayvalue[j][1]);
                    _feeorder.Save();
                }
            }

            
            

            return true;
        }
        [AOP(typeof(AopTransaction))]
        private object SaveItemFeeHead2(object value)
        {
            AbstractItemFeeHead head = (AbstractItemFeeHead)value;
            head.save();
            //先清除费用明细表记录    
            cashDao.DeleteItemFeeOrder(head.HeadId);
            AbstractItemFeeOrder order = NewObject<AbstractItemFeeOrder>();
            order.OrderID = null;
            order.HeadId = head.HeadId;
            order.OwnerId = head.OwnerId;
            order.OwnerName = head.OwnerName;
            order.SrccId = head.SrccId;
            order.SrccSonId = null;
            order.ItemCode = head.ItemCode;
            order.ItemName = head.ItemName;
            order.StatCode = NewObject<AbstractServiceItem>().GetServiceItem(head.ItemCode).StatCode;
            order.OrderDate = DateTime.Now;
            order.TotalFee = head.CostFee;
            order.Save();
            return true;
        }

        public override object OperationDataModel(string method, object value, CoreFrame.Core.Orm.PageInfo page)
        {
            switch (method)
            {
                case "SearchRoom":
                    string areacode = ((Hashtable)value)["areacode"].ToString();
                    string buildcode = ((Hashtable)value)["buildcode"].ToString();
                    string roomnumber = ((Hashtable)value)["roomnumber"].ToString();
                    return cashDao.SearchRoom(areacode, buildcode, roomnumber, page);
                case "SearchOwner":
                    string ownername = ((Hashtable)value)["ownername"].ToString();
                    string tel = ((Hashtable)value)["tel"].ToString();
                    string number = ((Hashtable)value)["number"].ToString();
                    return cashDao.SearchOwner(ownername, tel, number, page);
            }
            return null;
        }
    }
}
